﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PushSharp.Apple;
using PushSharp.Core;
using PushSharp;
using System.IO;
using ifunction.JPush.V3;

namespace BartonExtension
{
    public static class PushExtension
    {
        public static void pushIOSDeviceNotification(string pwd, string deviceToken, string alert, int badge, string sound)
        {
            var push = new PushBroker();
            var appleCert = File.ReadAllBytes(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "./PushCerts/PushSharp.PushCert.Development.p12"));
            push.RegisterAppleService(new ApplePushChannelSettings(false, appleCert, pwd));
            push.QueueNotification(new AppleNotification()
                                .ForDeviceToken(deviceToken)
                                .WithAlert(alert)
                                .WithBadge(badge)
                                .WithSound(sound));
        }

        public static void pushIOSDeviceNotification(string pwd, string deviceToken, string alert, object userinfo, int badge, string sound)
        {
            var push = new PushBroker();
            var appleCert = File.ReadAllBytes(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "./PushCerts/PushSharp.PushCert.Development.p12"));
            push.RegisterAppleService(new ApplePushChannelSettings(false, appleCert, pwd));
            push.QueueNotification(new AppleNotification()
                                .ForDeviceToken(deviceToken)
                                .WithAlert(alert)
                                .WithBadge(badge)
                                .WithCustomItem("userinfo", userinfo)
                                .WithSound(sound));
        }

        public static void JPushDeviceNotification(string notification)
        {
            //String result;
            String app_key = ConfigurationExtensions.ValueOfConfig("jpush_app_key");
            String master_secret = ConfigurationExtensions.ValueOfConfig("jpush_master_secret");
            //int sendno = 9;
            JPushClientV3 client = new JPushClientV3(app_key, master_secret);
            Audience au = new Audience();
            au.Add(PushTypeV3.Broadcast, null);
            ifunction.JPush.V3.Notification noti = new ifunction.JPush.V3.Notification()
            {
                iOSNotification = new iOSNotificationParameters()
                {
                    Badge = "+1",
                    Alert = notification,
                    Sound = "happy"
                }
            };
            client.SendPushMessage(new PushMessageRequestV3()
            {
                Audience = au,
                IsTestEnvironment = true,
                AppMessage = new AppMessage()
                {
                    Content = notification
                },
                Notification = noti
            });
        }

        public static void JPushDeviceNotification(string notification,string alias)
        {
            //String result;
            String app_key = ConfigurationExtensions.ValueOfConfig("jpush_app_key");
            String master_secret = ConfigurationExtensions.ValueOfConfig("jpush_master_secret");
            PushRequest req = new PushRequest();
            req.platform = "all";
            req.audience.Add("alias",new List<string>(new string[]{alias}));
            Dictionary<string,object> dic = new Dictionary<string,object>();
            dic.Add("alert",notification);
            dic.Add("sound","default");
            dic.Add("badge","+1");
           req.notification.Add("ios",dic);
            Dictionary<string,object> opt = new Dictionary<string,object>();
            opt.Add("time_to_live",60);
            opt.Add("apns_production",false);
            req.options = opt;
            HttpPost post = new HttpPost("https://api.jpush.cn/v3/push", req.ToJson());
            post.post((s, e) =>
                {
                    if(e == null)
                    {
                        Log.Info(s);
                    }
                }, new System.Net.NetworkCredential(app_key, master_secret));
        }
    }

    public class PushRequest
    {
        public string platform{get;set;}
        public Dictionary<string,object> audience{get;set;}
        public Dictionary<string,object> notification{get;set;}
        public Dictionary<string, object> options { get; set; }

        public PushRequest()
        {
            audience = new Dictionary<string, object>();
            notification = new Dictionary<string, object>();
        }
    }
}
